package net.lab1024.sa.base.utils;

import org.jasypt.encryption.StringEncryptor;
import org.jasypt.encryption.pbe.PooledPBEStringEncryptor;
import org.jasypt.encryption.pbe.config.SimpleStringPBEConfig;

/**
 * Jasypt 加密工具类
 * 用于配置文件中的密码加密和解密
 */
public class JasyptUtil {
    
    private static final String ALGORITHM = "PBEWithMD5AndDES";
    private static final String PASSWORD = "soc-web-2025-ti-cai-ke-ji"; // 加密密钥，建议从环境变量获取
    
    /**
     * 创建加密器
     */
    public static StringEncryptor createEncryptor() {
        PooledPBEStringEncryptor encryptor = new PooledPBEStringEncryptor();
        SimpleStringPBEConfig config = new SimpleStringPBEConfig();
        config.setPassword(PASSWORD);
        config.setAlgorithm(ALGORITHM);
        config.setKeyObtentionIterations("1000");
        config.setPoolSize("1");
        config.setProviderName("SunJCE");
        config.setSaltGeneratorClassName("org.jasypt.salt.RandomSaltGenerator");
        config.setStringOutputType("base64");
        encryptor.setConfig(config);
        return encryptor;
    }
    
    /**
     * 加密字符串
     */
    public static String encrypt(String plainText) {
        StringEncryptor encryptor = createEncryptor();
        return encryptor.encrypt(plainText);
    }
    
    /**
     * 解密字符串
     */
    public static String decrypt(String encryptedText) {
        StringEncryptor encryptor = createEncryptor();
        return encryptor.decrypt(encryptedText);
    }
    
    /**
     * 生成加密后的密码
     * 用于生成配置文件中的加密密码
     */
//    public static void main(String[] args) {
//        // 数据库密码
//        System.out.println("数据库密码加密: " + encrypt("1qazXSW@3edc"));
//
//        // Druid密码
//        System.out.println("Druid密码加密: " + encrypt("1024lab"));
//
//        // Kafka密码
//        System.out.println("Kafka密码加密: " + encrypt("8hasdAQU6!@#zXMP9A"));
//
//        // Redis密码（当前为空）
//        System.out.println("Redis密码加密: " + encrypt(""));
//
//        // 邮件密码
//        System.out.println("邮件密码加密: " + encrypt("LAB1024LAB"));
//    }
}
